﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace XYAC.Framework.MonitorTireMaterialManual.UC
{
    /// <summary>
    /// TableHeaderUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class LineUserControl : UserControl
    {
        public LineUserControl()
        {
            InitializeComponent();
            this.SizeChanged += TableHeaderUserControl_SizeChanged;
        }

        private void TableHeaderUserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DrawLine();
            //drawText(ActualWidth - 40, 10, "项目",Colors.DarkGreen , myCanvas);//Color.FromRgb(200, 0, 0)
            //drawText(10, ActualHeight - 30, "规格", Colors.DarkGreen, myCanvas);
        }

        private void drawText(double x, double y, string text, Color color, Canvas canvasObj)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = text;
            textBlock.Foreground = new SolidColorBrush(color);
            textBlock.FontSize = 18;
            Canvas.SetLeft(textBlock, x);
            Canvas.SetTop(textBlock, y);
            canvasObj.Children.Add(textBlock);
        }

        private void DrawLine()
        {
            Line line = new Line();
            Size rect = DesiredSize;// VisualTreeHelper.GetDescendantBounds(this);
            // 设置线条的起点和终点
            line.X1 = 1;
            line.Y1 = 1;

            line.X2 = ActualWidth-1; // 横向长度
            line.Y2 = ActualHeight-1; // 纵向长度

            // 设置线条的颜色和粗细
            line.Stroke = Brushes.DarkGreen;
            line.StrokeThickness = 2;
            myCanvas.Children.Clear();
            // 将线条添加到Canvas中
            myCanvas.Children.Add(line);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DrawLine();
            //drawText(ActualWidth - 40, 10, "项目", Colors.DarkGreen, myCanvas);//Color.FromRgb(200, 0, 0)
            //drawText(10, ActualHeight - 30, "规格", Colors.DarkGreen, myCanvas);
        }
        /*
        增加以下类代码， 跟踪尺寸，无果！
        static TableHeaderUserControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TableHeaderUserControl), new FrameworkPropertyMetadata(typeof(TableHeaderUserControl)));
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            // 在这里可以自定义控件的布局
            return base.ArrangeOverride(finalSize);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            // 在这里可以自定义控件的尺寸测量
            return new Size(100, 100);
        }
        */
    }
}
